package com.ittest.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ittest.domain.User;
import com.ittest.domain.Vo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {


    @RequestMapping(value = "quick24")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save24(String username, MultipartFile[] uploadFile) throws IOException {
        System.out.println(username);
        //获得上传文件的名称
        for (MultipartFile multipartFile : uploadFile) {
            String originalFilename = multipartFile.getOriginalFilename();
            multipartFile.transferTo(new File("/Users/admin/Desktop/upload/"+originalFilename));

        }
    }


    @RequestMapping(value = "quick23")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save23(String username, MultipartFile uploadFile,MultipartFile uploadFile2) throws IOException {
        System.out.println(username);
        //获得上传文件的名称
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("/Users/admin/Desktop/upload/"+originalFilename));
        String originalFilename2 = uploadFile2.getOriginalFilename();
        uploadFile.transferTo(new File("/Users/admin/Desktop/upload/"+originalFilename2));
    }


    @RequestMapping(value = "quick22",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save22(@CookieValue(value = "JSESSIONID") String jsessionId) throws IOException {
        System.out.println(jsessionId);
    }


    @RequestMapping(value = "quick21",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save21(@CookieValue(value = "JSESSIONID") String jsessionId) throws IOException {
        System.out.println(jsessionId);
    }

    @RequestMapping(value = "quick20",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save20(@RequestHeader(value = "User-Agent",required = false) String user_agent) throws IOException {
        System.out.println(user_agent);
    }


    @RequestMapping(value = "quick19",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save19(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }



    @RequestMapping(value = "quick18",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save18(Date date) throws IOException {
        System.out.println(date);
    }




    //localhots:8080/user/qcick17/zhangsan
    @RequestMapping(value = "quick17/{username}",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save17(@PathVariable(value = "username") String username) throws IOException {
        System.out.println(username);}


    @RequestMapping(value = "quick16",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save16(@RequestParam(value = "name",required = false,defaultValue = "杨闯杰") String username) throws IOException {
        System.out.println(username);}





        @RequestMapping(value = "quick15",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save15(@RequestBody List<User> userList) throws IOException {
        System.out.println(userList);

    }



    @RequestMapping(value = "quick14",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save14(Vo vo) throws IOException {
        System.out.println(vo);
    }


    @RequestMapping(value = "quick13",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save13(String strs[]) throws IOException {
        System.out.println(Arrays.asList(strs));

    }

    @RequestMapping(value = "quick12",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save12(User user) throws IOException {
        System.out.println(user);

    }



    @RequestMapping(value = "quick11",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public void save11(String username,int age) throws IOException {
        System.out.println(username);
        System.out.println(age);


    }



    @RequestMapping(value = "quick10",produces = "application/json;charset=UTF-8")
    @ResponseBody
    //期望springMvc将User转为json格式字符串
    public User save10() throws IOException {

        User user = new User();
        user.setUsername("萝北");
        user.setAge(21);

        return user;

    }




    @RequestMapping(value = "quick9",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String save9() throws IOException {

        User user = new User();
        user.setUsername("张三");
        user.setAge(18);

        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(user);

        //使用json的转换工具将对象转换为json格式字符串在返回
        return s;

    }




    @RequestMapping(value = "quick8")
    @ResponseBody
    public String save8() throws IOException {
        return "{\"username\":\"zhangsan\",\"age\":18}";

    }



    @RequestMapping(value = "quick7")
    @ResponseBody //告知springMvc框架 不进行视图跳转 直接进行数据响应
    public String save7( ) throws IOException {
       return "hello javaSpringMvc";

    }



    @RequestMapping(value = "quick6")
    public void save6(HttpServletResponse response ) throws IOException {
        response.getWriter().print("hello javaSpringMvc");

    }




    @RequestMapping(value = "quick5")
    public String save5(HttpServletRequest request){
        request.setAttribute("username","杨闯杰");
        return "success";
    }


    @RequestMapping(value = "quick4")
    public String save4(Model model){
        model.addAttribute("username","去哪儿网");
        return "success";
    }


    @RequestMapping(value = "quick3")
    public ModelAndView save3(ModelAndView modelAndView){
        modelAndView.addObject("username","ItTest");

        //设置视图
        modelAndView.setViewName("success");

        return  modelAndView;
    }



    @RequestMapping(value = "quick2")
    public ModelAndView save2(){

        /**
         * model：模型 作用封装数据
         * View； 视图 作用展示数据
         */
        ModelAndView modelAndView = new ModelAndView();
        //设置模型数据
        modelAndView.addObject("username","itcast");

        //设置视图
        modelAndView.setViewName("success");
        return  modelAndView;
    }



    @RequestMapping(value = "quick",method = RequestMethod.GET,params = {"username"})
    public String save(){
        System.out.println("Controller save running...");
        return "success";
    }
}
